<template>
   <div v-show="planType" class="app-container">
    <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="[ planaddBtn ]"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          v-if="isSubsidy"
          @click="poorTypeManagement"
          v-hasPermi="['poorType:plan:add']"
        >困难类型管理</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div v-if="isSchoolScholarship" style="line-height: 24px;">
        {{queryParams.yearStr}}年
    </div>
    <el-table :data="tableData" v-loading="loading">
        <template v-if="isSubsidy">
            <el-table-column label="年份" width="120" prop="yearStr" align="center" />
            <el-table-column label="困难生申请计划名称" prop="planName" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="计划名额总计" prop="planTotal" align="center" />
            <!-- <el-table-column v-for="item in diffTypePersonList" :key="item.typeId" :label="item.diffName + '名额'" :prop="'diffName'+ item.typeId" align="center" /> -->
            <el-table-column label="计划金额" prop="planMoneyTotal" align="center" />
            <el-table-column label="申请表" prop="applyFormUrl" align="center">
                <template slot-scope="{row}">
                    <el-button type="text" v-if="row.applyFormUrl" @click="downloadApply(row.applyFormUrl, row.applyFormName)">{{row.applyFormName}}</el-button>
                    <el-button type="text" v-hasPermi="['poor:plan:edit']"  v-else @click="uploadApply('SQ', row)">上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="" width="60" align="left">
                <template slot-scope="{row}">
                    <el-button type="text" v-hasPermi="['poor:plan:edit']" v-if="row.applyFormUrl" @click="uploadApply('SQ', row)">重新上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="认定表" prop="identifyFormUrl" align="center">\
                <template slot-scope="{row}">
                    <el-button type="text" v-if="row.identifyFormUrl" @click="downloadApply(row.identifyFormUrl, row.identifyFormName)">{{row.identifyFormName}}</el-button>
                    <el-button type="text" v-hasPermi="['poor:plan:edit']" v-else @click="uploadApply('RD', row)">上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="" width="60" align="left">
                <template slot-scope="{row}">
                    <el-button type="text"  v-hasPermi="['poor:plan:edit']" v-if="row.identifyFormUrl" @click="uploadApply('RD', row)">重新上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime" align="center" />
        </template>
        <template v-if="isScholarship">
            <el-table-column label="年份" width="120" prop="yearStr" align="center" />
                <el-table-column prop="personNumber" label="本科名额" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="personNumber2" label="专科名额" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column label="合计名额" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        {{ totalFun(scope.row.personNumber, scope.row.personNumber2) }}
                    </template>
                </el-table-column>
            <el-table-column label="金额" prop="money" align="center" />
            <el-table-column label="申请表" :show-overflow-tooltip="true" align="center">
                <template slot-scope="{row}">
                    <el-button type="text" v-if="row.fileUrl" @click="downloadApply(row.fileUrl, row.fileName)">{{row.fileName}}</el-button>
                    <el-button type="text" v-hasPermi="[planeditBtn]" v-else @click="uploadApply(planType+'Plan', row)">点击上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="" width="60" align="left">
                <template slot-scope="{row}">
                    <el-button type="text" v-hasPermi="[planeditBtn]" v-if="row.fileUrl" @click="uploadApply(planType+'Plan', row)">重新上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime" align="center" />
        </template>

        <template v-if="isSchoolScholarship">
            <el-table-column label="顺序" prop="sort" width="60" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="奖学金名称" prop="typeName" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="奖学金金额" prop="bonusMoney" align="center" />
            <el-table-column label="奖学金名额" prop="personNum" align="center" />
            <!-- <el-table-column label="申请表" prop="fileUrl" align="center">
                <template slot-scope="{row}">
                    <el-button type="text" v-if="row.fileUrl" @click="downloadApply(row.fileUrl, row.fileName)">{{row.fileName}}</el-button>
                    <el-button type="text" v-hasPermi="['poor:plan:edit']"  v-else @click="uploadApply('schoolScholarshipSQ', row)">上传</el-button>
                </template>
            </el-table-column>
            <el-table-column label="" width="60" align="left">
                <template slot-scope="{row}">
                    <el-button type="text" v-hasPermi="['poor:plan:edit']" v-if="row.fileUrl" @click="uploadApply('schoolScholarshipSQ', row)">重新上传</el-button>
                </template>
            </el-table-column> -->
            <el-table-column label="创建时间" prop="createTime" align="center" />
            <el-table-column label="是否二级学院可以导入配置" prop="isSet" align="center">
                <template slot-scope="{row}">
                    {{row.isSet == "1"?"是":"否"}}
                </template>
            </el-table-column>
            <el-table-column label="奖项不可兼得" prop="isHaveBoth" align="center">
                <template slot-scope="{row}">
                    {{row.isHaveBoth == "1"?"是":"否"}}
                </template>
            </el-table-column>
             <el-table-column label="四大组织" prop="orgFlag" align="center">
                <template slot-scope="{row}">
                    {{row.orgFlag == "1"?"是":"否"}}
                </template>
            </el-table-column>
        </template>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                v-hasPermi="[ planeditBtn ]"
                @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
                size="mini"
                type="text"
                v-hasPermi="[ plandelBtn ]"
                @click="handleDelete(scope.row)"
            >删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <AddPlan ref="addplanRef" @onSearch="onSearch"></AddPlan>
    <ScholarshipAdd ref="scholarshipAddRef" @onSearch="onSearch"></ScholarshipAdd>
    <PoorType ref="poorTypeRef" @onSearch="onSearch"></PoorType>
    <UploadFile ref="uploadFileRef" @onSearch="onSearch"></UploadFile>
    <SchoolScholarshipAdd ref="schoolScholarshipAddRef" @onSearch="onSearch"></SchoolScholarshipAdd>
    <SchoolScholarshipEdit ref="schoolScholarshipEditRef" @onSearch="onSearch"></SchoolScholarshipEdit>
   </div>
</template>

<script>
import searchForm from "@/components/Search/searchForm.vue";
import AddPlan from "./modules/poorAdd.vue";
import ScholarshipAdd from "./modules/scholarshipAdd.vue";
import SchoolScholarshipAdd from "./modules/schoolScholarshipAdd.vue";
import SchoolScholarshipEdit from "./modules/schoolScholarshipEdit.vue";
import UploadFile from "./modules/uploadFile.vue";
import PoorType from "./modules/poorType.vue";
import { 
    scholarshipPlanList,
    removeScholarshipPlan,
    poorStudentList,
    delPoorStudent,
    schoolScholarshipList,
    schoolScholarshipDel
 } from "@/api/poorStudent-scholarship/plan.js";
import {
    scholarshipTypeArr
} from "@/views/poorStudent-scholarship/dict.js"
export default {
    name:"planManagement",
    components: { 
        searchForm,
        AddPlan,
        PoorType,
        ScholarshipAdd,
        UploadFile,
        SchoolScholarshipAdd,
        SchoolScholarshipEdit
    },
    data () {
        return{
            formLabelWidth: "60px",
            formOptions: [
                {
                    label: "年份",
                    prop: "yearStr",
                    type: "year",
                    valueFormat: "yyyy",
                    element: "el-date-picker",
                    placeholder: "请选择年份"
                }
            ],
            // 显示搜索条件
            showSearch: true,
            // 查询参数
            queryParams: {

            },
            tableData: [],
            // 遮罩层
            loading: true,
            diffTypePersonList: [],
            scholarshipTypeArr
        }
    },
    computed:{
        planType () {
            return this.$route.params && this.$route.params.type
        },
        isSubsidy(){
            return this.planType === 'subsidy'
        },
        // 国家奖学金
        isScholarship(){
            return this.scholarshipTypeArr.some((item) => {
                return item.code == this.planType 
            })
        },
        scholarshipType(){
            const scholarshipTypeItem = this.scholarshipTypeArr.find((item) => {
                return item.code == this.planType 
            })
            if (scholarshipTypeItem) {
                return scholarshipTypeItem.typeId
            } else {
                return 0
            }
        },
        isSchoolScholarship(){
            return this.planType === 'schoolScholarship'
        },
        planaddBtn () {
            if (this.isSubsidy) {
                return 'poor:plan:add'
            }
            if (this.isScholarship) {
                return `${this.planType}:plan:add`
            }
            if (this.isSchoolScholarship) {
                return 'schoolScholarship:plan:add'
            }
        },
        planeditBtn () {
            if (this.isSubsidy) {
                return 'poor:plan:edit'
            }
            if (this.isScholarship) {
                return `${this.planType}:plan:edit`
            }
            if (this.isSchoolScholarship) {
                return 'schoolScholarship:plan:edit'
            }
        },
        plandelBtn () {
            if (this.isSubsidy) {
                return 'poor:plan:del'
            }
            if (this.isScholarship) {
                return `${this.planType}:plan:del`
            }
            if (this.isSchoolScholarship) {
                return 'schoolScholarship:plan:del'
            }
        }
    },
    created(){
        if (this.isSchoolScholarship) {
            this.formOptions[0].initValue = new Date().getFullYear() + ''
            this.queryParams.yearStr = new Date().getFullYear() + ''
        }
        this.getList()
    },
    methods:{
        totalFun(a, b) {
            if (a && b) {
                const _total = Number(a) + Number(b)
                if (isNaN(_total)) {
                    return ''
                } else {
                    return _total
                }
            } else {
                return ''
            }
        },
        downloadApply (url, filename) {
            const link = document.createElement("a");
            const body = document.querySelector("body");
            link.href = url
            link.download = filename;
            link.style.display = "none";
            body.appendChild(link);
            link.click();
            body.removeChild(link);
        },
        uploadApply (type, rowdata) {
            this.$refs.uploadFileRef.open(type, rowdata)
        },
        poorTypeManagement(){
            this.$refs.poorTypeRef.open()
        },
        onSearch(val) {
            console.log(val)
            // 查询条件和分页数据合并
            this.queryParams = Object.assign(this.queryParams, val);
            this.getList();
        },
         // 重置
        onReset(data) {
            this.queryParams = Object.assign(this.queryParams, data);
            this.getList();
        },
        getList () {
            this.loading = true;
            if (this.isSubsidy) {
                poorStudentList(this.queryParams).then((res) => {
                    res.rows.forEach((element, index) => {
                        if (index === 0) {
                            this.diffTypePersonList = element.diffTypePersonList
                        }

                        if (element.diffTypePersonList && element.diffTypePersonList.length > 0) {
                            let _planTotal = 0
                            let _planMoneyTotal = 0
                            element.diffTypePersonList.forEach((item) => {
                                _planTotal = _planTotal + Number(item.personNum)
                                _planMoneyTotal = _planMoneyTotal + Number(item.personNum) * Number(item.diffMoney)
                                element['diffName'+item.typeId] = item.personNum
                            })
                            element.planTotal = _planTotal
                            element.planMoneyTotal = _planMoneyTotal
                        } else {
                            element.planTotal = 0
                            element.planMoneyTotal = 0
                        }
                        
                    });
                    this.tableData = res.rows
                    console.log(this.tableData, 'this.tableData')
                    this.loading = false;
                })
            }
            if (this.isScholarship) {
                scholarshipPlanList({
                    ...this.queryParams,
                    scholarshipType: this.scholarshipType
                }).then((res) => {
                    this.tableData = res.rows
                    this.loading = false;
                })
            }
            if (this.isSchoolScholarship) {
                schoolScholarshipList(this.queryParams).then((res) => {
                    this.tableData = res.rows
                    this.loading = false;
                })
            }
        },
        handleAdd(){
            if (this.isSubsidy) {
                this.$refs.addplanRef.open('add')
            }
            if (this.isScholarship) { 
                this.$refs.scholarshipAddRef.open('add', {}, this.scholarshipType)
            }
            
            if (this.isSchoolScholarship) {
                this.$refs.schoolScholarshipAddRef.open(this.queryParams.yearStr)
            }
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            row = JSON.parse(JSON.stringify(row))
            if (this.isSubsidy) {
                this.$refs.addplanRef.open('edit', row)
            }
            if (this.isScholarship) { 
                this.$refs.scholarshipAddRef.open('edit',row)
            }
            if (this.isSchoolScholarship) {
                this.$refs.schoolScholarshipEditRef.open(row)
            }
        },
        handleDelete(row) {
            let deleteTitle = ""
            if (this.isSubsidy) {
                deleteTitle = '该计划已经上传了困难生，如删除后该部分数据将同步删除，请确认是否删除?'
            }
            if (this.isScholarship || this.isSchoolScholarship) { 
                deleteTitle = '请确认是否删除?'
            }
            this.$modal
                .confirm(deleteTitle)
                .then(() => {
                    if (this.isSubsidy) {
                        return delPoorStudent(row.id)
                    }
                    if (this.isScholarship) { 
                        return removeScholarshipPlan(row.id)
                    }
                    if (this.isSchoolScholarship) {
                        return schoolScholarshipDel(row.id)
                    }
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
    }
}
</script>

<style>

</style>